package cn.davidsu.library;


import cn.davidsu.library.util.LogUtil;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.media.image.PixelMap;

public class CustomShadowBackground extends PixelMapElement {
    
    private int mColor;
    private int mShadowColor;
    private int[] mGradientColorArray;
    private float[] mGradientPositions;
    private Shader shader;

    private int mRadius;
    private int mShadowRadius;

    private int mOffsetX;
    private int mOffsetY;
    private RectFloat mRectF;
    private Paint mPaint;


    protected CustomShadowBackground(int color, int[] colorArray, float[] gradientPositions, int shadowColor, Shader shader,
                                     int radius, int shadowRadius, int offsetX, int offsetY, PixelMap pixelMap) {
        super(pixelMap);
        try {
            this.mColor = color;
            this.mGradientColorArray = colorArray;
            this.mGradientPositions = gradientPositions;
            this.mShadowColor = shadowColor;
            this.shader = shader;

            this.mRadius = radius;
            this.mShadowRadius = shadowRadius;

            this.mOffsetX = offsetX;
            this.mOffsetY = offsetY;
            Canvas canvas = new Canvas(new Texture(pixelMap));
            if (mRectF == null && pixelMap != null) {
                Rect bounds = new Rect(0,0, pixelMap.getImageInfo().size.width, pixelMap.getImageInfo().size.height);
                mRectF = new RectFloat(bounds.left + mShadowRadius - mOffsetX, bounds.top + mShadowRadius - mOffsetY, bounds.right - mShadowRadius - mOffsetX,
                        bounds.bottom - mShadowRadius - mOffsetY);
            }

            if (mPaint == null) {
                initPaint();
            }

            canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
            LogUtil.i("create instance");
        } catch (Exception e) {
            LogUtil.i("error msg: %s", e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    public void setColorFilter(ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        BlurDrawLooper blurDrawLooper = new BlurDrawLooper(mShadowRadius, mOffsetX, mOffsetY, new Color(mShadowColor));
        mPaint.setBlurDrawLooper(blurDrawLooper);

        if (mGradientColorArray != null && mGradientColorArray.length > 1) {
//            boolean isGradientPositions = mGradientPositions != null && mGradientPositions.length > 0 && mGradientPositions.length == mGradientColorArray.length;

//            mPaint.setShader(mLinearGradient == null ? new LinearGradient(mRectF.left, 0, mRectF.right, 0, mGradientColorArray,
//                    isGradientPositions ? mGradientPositions : null, Shader.TileMode.CLAMP) : mLinearGradient);
            Point[] points = new Point[]{new Point(0, mRectF.bottom/2), new Point(mRectF.right, mRectF.bottom/2)};
            shader = shader == null ? new LinearShader(points, mGradientPositions, toColorArray(mGradientColorArray), Shader.TileMode.CLAMP_TILEMODE)  : shader;
            mPaint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
        } else {

            mPaint.setColor(new Color(mColor));
        }
    }

    private Color[] toColorArray(int[] colors){
        Color[] colorArray = new Color[colors.length];
        for (int i=0; i<colors.length; i++){
            colorArray[i]=new Color(colors[i]);
        }
        return colorArray;
    }


}
